#include "LoginUi.h"
#include "ui_LoginUi.h"
#include "WidgetHandler.h"
#include "BootSetting.h"
#include "LogoUtility.h"
#include "HintDialog.h"
#include "Mcu.h"

LoginUi::LoginUi(QWidget *parent) :
    BaseWidget(parent),
    ui(new Ui::LoginUi)
{
    ui->setupUi(this);
    ui->password->setEchoMode(QLineEdit::Password);
    LogoUtility::getInstance()->addLogoLabel(ui->logo);
}

LoginUi::~LoginUi()
{
    delete ui;
}

void LoginUi::activate()
{

}

void LoginUi::inactivate()
{

}

void LoginUi::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}

void LoginUi::on_login_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    if (ui->password->text().isEmpty() || ui->password->text().isNull()) {
        HintDialog::getInstance()->setInfo(tr("密码不能为空!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return;
    }
    if (ui->password->text() != "888888") {
        HintDialog::getInstance()->setInfo(tr("密码错误!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return;
    }
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId = 0;
    if (ui->loginModo->isChecked()) {
        RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.loginMode = 1;
    } else {
        RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.loginMode = 0;
    }
    RuntimeData::BootSetting::getInstance()->saveBootSettingInfoToRom();

    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void LoginUi::on_skip_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId = -1;
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.loginMode = 0;
    RuntimeData::BootSetting::getInstance()->saveBootSettingInfoToRom();

    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}
